# -*- coding: utf-8 -*-
# @Time    : 2018/9/30 9:08
import os
import sys
path = os.path.abspath(os.path.dirname(os.getcwd()))
sys.path.append(path)
import pymongo,config,pymssql
from mongoFild import insert_mongo
from hashlib import md5
from collections import OrderedDict
# db = pymongo.MongoClient(config.MONGODB_URI)[config.MONGODB_NAME]
# 关键词mogog
# key_mongo = db[config.WORDS_NAME]

class txt_Mongo(object):
    def __init__(self) -> None:
        super().__init__()

    def find_key(self):
        # connects_two = pymssql.connect('192.168.2.212', 'sa', '123456', 'words', charset='utf8')
        # cursor_two = connects_two.cursor()
        # sql = "select word from word1"
        # cursor_two.execute(sql)
        # # result = cursor_two.fetchone()
        # for i in cursor_two:
        #     # print(i[0])
        #     insert_mongo().insert_mongo_word(i[0])
        #从文档里拿数据入mongodb
        db = pymongo.MongoClient('mongodb://192.168.2.211:27017')
        con_proxy = db['proxy']
        # db_one = config.con_proxy
        li_db_one = con_proxy['proxies_ip']
        with open('../ip.txt','r') as f:#rb读出的是字节
            lines = f.readlines()
            # line = (line[:-1]).replace("\r","").replace("\n","")
            # print(line)
            for f in lines:
                # line = f.decode()
                ip = (f[:-1]).replace("\r", "").replace("\n", "")
                ip_md5 = self.md5_generator(ip)
                save = OrderedDict()
                save['ip'] = ip
                save['ip_md5'] = ip_md5
                save['status'] = 0
                try:
                    li_db_one.insert(save)
                except Exception as e:
                    print(e)
                # insert_mongo().insert_mongo_word(line.strip())
            f.close()

    def md5_generator(self,url):
        return md5(url.encode()).hexdigest()

if __name__ == '__main__':
    txt_Mongo().find_key()